/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package IFSv2.Forms.Main;

import java.awt.Component;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;

/**
 *
 * @author Joel
 */
public class MainToolBar extends javax.swing.JToolBar{
    
    private HashMap<String, JButton> buttonMap = new HashMap();
    
    public MainToolBar(){
        setFloatable(false);
        setDefaultButtons();
        reloadButtonMap();
    }
    /*Default Toolbar Buttons*/
    static JButton btnNew;
    static JButton btnEdit;
    static JButton btnSave;
    static JButton btnCancel;
    static JButton btnDelete;
    static JButton btnPrint;
    static JButton btnCurrency;
    
    private void setDefaultButtons(){
        
        btnNew = createButton("New", "new");
        add(btnNew);
        btnEdit = createButton("Edit", "edit");
        add(btnEdit);
        btnSave = createButton("Save", "save");
        add(btnSave);
        btnCancel = createButton("Cancel", "cancel");
        add(btnCancel);
        btnDelete = createButton("Delete", "delete");
        add(btnDelete);
        btnPrint = createButton("Print", "print");
        add(btnPrint);
//        addSeparator();
//        btnCurrency = createButton("Currency", "currency");
//        add(btnCurrency);
    
    }
        
    private void reloadButtonMap(){
        buttonMap.clear();
        for(Component component: getComponents()){
            if(component instanceof JButton){
                buttonMap.put(component.getName(), (JButton)component);
            }
        }
    }
    
    public JButton createButton(String text, String imageName){
    
        ToolBarButtonListener toolBarButtonListener = new ToolBarButtonListener();
        
        final JButton button = new JButton(text);
        setupToolButton(button, text, imageName, "btn"+text);
        button.addMouseListener(toolBarButtonListener);
        return button;
        
    }
    
    private void setupToolButton(JButton jButton, String text, String imageName , String variableName){
        jButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/IFSv2/Images/"+imageName+".png")));
        jButton.setEnabled(false);
        jButton.setFocusable(false);
        jButton.setName(variableName);
        jButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    }
    
    public static void enableButtons(String mode){
        if(mode == null) return;
        
        switch(mode){
            case "ADD":
            case "EDIT":
                btnNew.setEnabled(false);
                btnEdit.setEnabled(false);
                btnDelete.setEnabled(false);
                btnSave.setEnabled(true);
                btnCancel.setEnabled(true);
                btnPrint.setEnabled(false);
                break;
            case "SAVE":
            case "VIEW":
                btnNew.setEnabled(true);
                btnEdit.setEnabled(true);
                btnDelete.setEnabled(true);
                btnSave.setEnabled(false);
                btnCancel.setEnabled(false);
                btnPrint.setEnabled(true);
                break;
            case "DEFAULT":
                btnNew.setEnabled(true);
                btnEdit.setEnabled(false);
                btnDelete.setEnabled(false);
                btnSave.setEnabled(false);
                btnCancel.setEnabled(false);
                btnPrint.setEnabled(false);
            default:
                break;
        }
    }
    
    public enum TRANSACTION_TYPE{
        ADD, EDIT, DELETE, PRINT, DEFAULT
    }
    
    public class ToolBarButtonListener implements MouseListener{
        
        @Override
        public void mouseClicked(MouseEvent e) {
            if(((JButton)e.getSource()).isEnabled()){
                Class params[] = new Class[1];
                params[0] = String.class;
                try {
                    frmMain.menuTabbedPane.getSelectedComponent().getClass().getDeclaredMethod("buttonClicked", params).invoke(frmMain.menuTabbedPane.getSelectedComponent(), ((JButton)e.getSource()).getName());
                } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
                    Logger.getLogger(MainToolBar.class.getName()).log(Level.SEVERE, null, ex);
                } catch ( NoSuchMethodException | SecurityException ex) {
                    Logger.getLogger(MainToolBar.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {}

        @Override
        public void mouseReleased(MouseEvent e) {}

        @Override
        public void mouseEntered(MouseEvent e) {}

        @Override
        public void mouseExited(MouseEvent e) {}
    
    }
    
    
}
